import store from '@/store'
import type { Token } from '@/types/http'

// Token key
const TOKEN_KEY = 'm-toutiao-token-key'

// 取
export const getToken = () => JSON.parse(
  localStorage.getItem(TOKEN_KEY) ?? '{ "token":"", "refresh_token": "" }'
) as Token

// 存
export const setToken = (token: Token) => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(token))
}

// 删
export const removeToken = () => {
  localStorage.removeItem(TOKEN_KEY)
}

// 是否授权
export const isAuth = () => !!store.getState().login.token